<?php
namespace org\kahikai\database\mysql;

require_once dirname(__FILE__) . '/../LanguageDao.php';
require_once dirname(__FILE__) . '/KahikaiMysql.php';
require_once dirname(__FILE__) . '/../../domain/Language.php';

use org\kahikai\database\LanguageDao;
use org\kahikai\domain\Language;

/**
 * 
 * @author ch4mp
 *
 */
class MysqlLanguageDao implements LanguageDao {
// 	private $byIdQuery;
	
	public function __construct() {
// 		$this->byIdQuery = KahikaiMysql::prepare('SELECT name FROM LANGUAGE WHERE id=?');
	}
	
	/**
	 *
	 * @return an array with all Languages
	 */
	public function getAll() {
		$languages = array();
		
		$result = KahikaiMysql::query('SELECT id, name FROM LANGUAGE');
		while ($record = $result->fetch_assoc()) {
			$languages[$record['id']] = new Language($record['id'], $record['name']);
		}
		$result->close();
		
		return $languages;
	}
	
	/**
	 *
	 * @param int $id a Language id
	 * @return the Language with provided id or null
	 */
	public function getById($id) {
		$result = KahikaiMysql::query("SELECT name FROM LANGUAGE WHERE id=$id");
		$record = $result->fetch_assoc();
		$result->close();
		return new Language($id, $record['name']);
// 		$this->byIdQuery->bind_param('i', $id);
// 		$this->byIdQuery->execute();
// 		$this->byIdQuery->bind_result($name);
// 		$this->byIdQuery->fetch();
// 		$this->byIdQuery->close();
// 		return new Language($id, $name);
	}
}
?>